﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.AlbLab.GEP.Dao.Base;
using Com.AlbLab.GEP.Dao.Interface;
using Com.AlbLab.GEP.Domain.Entities;
using Oracle.DataAccess.Client;
using Com.AlbLab.GEP.Domain;
using System.Data;

namespace Com.AlbLab.GEP.Dao.Impl
{
    public class PraticaM01Dao : OracleDao<OracleSessionManager, ISessionModel>, IPraticaM01Dao
    {
        private string insert = "INSERT INTO GEP_DATIM01"
                              + "(DM01_COD_PRATICA, DM01_LETRECL_AF1, DM01_LETRECL_AF2, DM01_LETRECL_AF3, DM01_LETRECL_RF1, DM01_LETRECL_RF2, DM01_LETRECL_RF3, DM01_LETRECL_PF1, DM01_LETRECL_PF2, DM01_LETRECL_PF3, DM01_DT_LET_RECL,"
                              + "DM01_TIP_LET_RECL, DM01_NEW_RECL, DM01_CODRECLAMO, DM01_LETCLI_AF1, DM01_LETCLI_AF2, DM01_LETCLI_AF3, DM01_LETCLI_RF1, DM01_LETCLI_RF2, DM01_LETCLI_RF3, DM01_LETCLI_PF1, DM01_LETCLI_PF2, DM01_LETCLI_PF3,"
                              + "DM01_DT_LET_CLI)"
                              + " VALUES(:DM01_COD_PRATICA, :DM01_LETRECL_AF1, :DM01_LETRECL_AF2, :DM01_LETRECL_AF3, :DM01_LETRECL_RF1, :DM01_LETRECL_RF2, :DM01_LETRECL_RF3, :DM01_LETRECL_PF1, :DM01_LETRECL_PF2, :DM01_LETRECL_PF3, :DM01_DT_LET_RECL,"
                              + ":DM01_TIP_LET_RECL, :DM01_NEW_RECL, :DM01_CODRECLAMO, :DM01_LETCLI_AF1, :DM01_LETCLI_AF2, :DM01_LETCLI_AF3, :DM01_LETCLI_RF1, :DM01_LETCLI_RF2, :DM01_LETCLI_RF3, :DM01_LETCLI_PF1, :DM01_LETCLI_PF2, :DM01_LETCLI_PF3," 
                              + ":DM01_DT_LET_CLI)";

        private string selectById = "select * from GEP_DATIM01 WHERE DM01_COD_PRATICA = :DM01_COD_PRATICA";

        public PraticaM01Dao(OracleSessionManager daoContext)
            : base(daoContext)
        {
            if (!base.Context.Session_isActive())
            {
                base.Context.Session_Init();
                base.CurrentConnection.Open();
            }
        }

        private OracleParameter[] MappingParams(GEP_DATIM01 r, bool isInsert)
        {
            OracleParameter[] oparams;
            oparams = new OracleParameter[24];

            oparams[0] = new OracleParameter("DM01_COD_PRATICA", OracleDbType.Decimal, r.Id_dm01_cod_pratica, ParameterDirection.Input);
            oparams[1] = new OracleParameter("DM01_LETRECL_AF1", OracleDbType.Decimal, r.Dm01_Letrecl_Af1, ParameterDirection.Input);
            oparams[2] = new OracleParameter("DM01_LETRECL_AF2", OracleDbType.Decimal, r.Dm01_Letrecl_Af2, ParameterDirection.Input);
            oparams[3] = new OracleParameter("DM01_LETRECL_AF3", OracleDbType.Decimal, r.Dm01_Letrecl_Af3, ParameterDirection.Input);
            oparams[4] = new OracleParameter("DM01_LETRECL_RF1", OracleDbType.Decimal, r.Dm01_Letrecl_Rf1, ParameterDirection.Input);
            oparams[5] = new OracleParameter("DM01_LETRECL_RF2", OracleDbType.Decimal, r.Dm01_Letrecl_Rf2, ParameterDirection.Input);
            oparams[6] = new OracleParameter("DM01_LETRECL_RF3", OracleDbType.Decimal, r.Dm01_Letrecl_Rf3, ParameterDirection.Input);
            oparams[7] = new OracleParameter("DM01_LETRECL_PF1", OracleDbType.Decimal, r.Dm01_Letrecl_Pf1, ParameterDirection.Input);
            oparams[8] = new OracleParameter("DM01_LETRECL_PF2", OracleDbType.Decimal, r.Dm01_Letrecl_Pf2, ParameterDirection.Input);
            oparams[9] = new OracleParameter("DM01_LETRECL_PF3", OracleDbType.Decimal, r.Dm01_Letrecl_Pf3, ParameterDirection.Input);
            oparams[10] = new OracleParameter("DM01_DT_LET_RECL", OracleDbType.Date, r.Dm01_Dt_Let_Recl, ParameterDirection.Input);
            oparams[11] = new OracleParameter("DM01_TIP_LET_RECL", OracleDbType.Varchar2, r.Dm01_Tip_Let_Recl, ParameterDirection.Input);
            oparams[12] = new OracleParameter("DM01_NEW_RECL", OracleDbType.Varchar2, r.Dm01_New_Recl, ParameterDirection.Input);
            oparams[13] = new OracleParameter("DM01_CODRECLAMO", OracleDbType.Varchar2, r.Dm01_Codreclamo, ParameterDirection.Input);
            oparams[14] = new OracleParameter("DM01_LETCLI_AF1", OracleDbType.Decimal, r.Dm01_Letcli_Af1, ParameterDirection.Input);
            oparams[15] = new OracleParameter("DM01_LETCLI_AF2", OracleDbType.Decimal, r.Dm01_Letcli_Af2, ParameterDirection.Input);
            oparams[16] = new OracleParameter("DM01_LETCLI_AF3", OracleDbType.Decimal, r.Dm01_Letcli_Af3, ParameterDirection.Input);
            oparams[17] = new OracleParameter("DM01_LETCLI_RF1", OracleDbType.Decimal, r.Dm01_Letcli_Rf1, ParameterDirection.Input);
            oparams[18] = new OracleParameter("DM01_LETCLI_RF2", OracleDbType.Decimal, r.Dm01_Letcli_Rf2, ParameterDirection.Input);
            oparams[19] = new OracleParameter("DM01_LETCLI_RF3", OracleDbType.Decimal, r.Dm01_Letcli_Rf3, ParameterDirection.Input);
            oparams[20] = new OracleParameter("DM01_LETCLI_PF1", OracleDbType.Decimal, r.Dm01_Letcli_Pf1, ParameterDirection.Input);
            oparams[21] = new OracleParameter("DM01_LETCLI_PF2", OracleDbType.Decimal, r.Dm01_Letcli_Pf2, ParameterDirection.Input);
            oparams[22] = new OracleParameter("DM01_LETCLI_PF3", OracleDbType.Decimal, r.Dm01_Letcli_Pf3, ParameterDirection.Input);
            oparams[23] = new OracleParameter("DM01_DT_LET_CLI", OracleDbType.Date, r.Dm01_Dt_Let_Cli, ParameterDirection.Input);
            
            //if (isInsert)
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Output);
            //else
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Input);
            return oparams;
        }

        #region IDao<GEP_DATIM01,decimal> Members

        public Dictionary<decimal, GEP_DATIM01> GetAll()
        {
            throw new NotImplementedException();
        }

        public GEP_DATIM01 GetById(decimal id)
        {
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectById, CurrentConnection);
            cmd.Parameters.Add("DM01_COD_PRATICA", id);
            GEP_DATIM01 ut = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                    {
                        r.Read();
                        ut = DaoOracleDbHelper.MapDatiM01(r);
                    }
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-D01003", e0x);
            }
            return ut;
        }

        public void Insert(GEP_DATIM01 entity)
        {
            using (OracleCommand ocmd = new OracleCommand())
            {
                try
                {
                    ocmd.CommandText = insert;
                    ocmd.Parameters.AddRange(MappingParams(entity, false));
                    ocmd.Connection = base.CurrentConnection;
                    ocmd.ExecuteNonQuery();
                   // entity.Id_Tes_Cod_Pratica = (decimal)ocmd.Parameters["pID"].Value;
                }
                catch (Exception e0x)
                {
                    throw new ManagedException("EX-PR004", e0x);
                }
            }
        }

        public void Update(GEP_DATIM01 entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(decimal id)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (!base.Context.TransactionModeOn)
            {
                base.CurrentConnection.Close();
            }
        }

        #endregion
    }
}